package org.redmagic.service.controller;

import java.net.UnknownHostException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.redmagic.service.MagicHierarchicalService;
import org.redmagic.service.utils.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.base.Optional;

@RequestMapping("/property")
@Controller
public class PropertyController extends AbstractLinkedDataController {

	private static final Log LOG = LogFactory.getLog(PropertyController.class);
	
	@Autowired
	private MagicHierarchicalService magicService;
	
	/**
	 * Used to retrieve existing properties.
	 */
	@RequestMapping(value = "/**", method = RequestMethod.GET)
	public @ResponseBody ResponseEntity<String> readProperty(HttpServletRequest request) throws UnknownHostException {

		String property = ServletUtils.getProperty(request, "/property/");
		List<String> tags = ServletUtils.getTags(request);
		String user = ServletUtils.getHostname(request);

		if (LOG.isInfoEnabled()) {
			LOG.info("[GET] " + property + "with " + tags + " for user: " + user);
		}
		
		
		Optional<String> rval = magicService.getProperty(property, tags, user);
		if (rval.isPresent()) {
			return new ResponseEntity<String>(rval.get(), HttpStatus.OK);
		} else {
			return new ResponseEntity<String>(HttpStatus.NOT_FOUND);
		}
	}
	
//	/**
//	 * Used to create need properties.
//	 * @throws URISyntaxException 
//	 */
//	@RequestMapping(value = "/**", method = RequestMethod.POST)
//	public @ResponseBody ResponseEntity<String> createProperty(HttpServletRequest request, @RequestBody JSONObject json) throws UnknownHostException, URISyntaxException {
//		
//		// get the requesting server
//		String user = ServletUtils.getHostname(request);
//		Property property = getProperty(json, "POST", user);
//		boolean result = magicService.createProperty(property, user);
//		
//		if (result) {
//			String propertyString = getLinkedDataProperty(request, "property", property);
//			HttpHeaders headers = new HttpHeaders();
//			headers.setLocation(new URI(propertyString));
//			return new ResponseEntity<String>(headers, HttpStatus.CREATED);
//		} else {
//			return new ResponseEntity<String>(HttpStatus.BAD_REQUEST);
//		}
//	}
	
//	/**
//	 * Used to update existing properties.
//	 */
//	@RequestMapping(value = "/**", method = RequestMethod.PUT)
//	public @ResponseBody ResponseEntity<String> updateProperty(HttpServletRequest request, @RequestBody JSONObject json) throws UnknownHostException {
//		
//		// get the requesting server
//		String user = ServletUtils.getHostname(request);
//		Property property = getProperty(json, "PUT", user);
//		boolean result = magicService.updateProperty(property, user);
//		
//		if (result) {
//			return new ResponseEntity<String>(HttpStatus.OK);
//		} else {
//			return new ResponseEntity<String>(HttpStatus.BAD_REQUEST);
//		}
//	}
	
//	private Property getProperty(JSONObject json, String method, String user) {
//		Gson gson = new GsonBuilder().disableHtmlEscaping().create();
//		Property property = gson.fromJson(json.toString(), Property.class);
//		if (LOG.isInfoEnabled()) {
//			LOG.info("[" + method + "] " + property + " for user: " + user);
//		}
//		return property;
//	}

	public MagicHierarchicalService getMagicService() {
		return magicService;
	}

	public void setMagicService(MagicHierarchicalService magicService) {
		this.magicService = magicService;
	}
}